/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sgbag1.Controlleur;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import sgbag1.Noyau.Aeroport;
import sgbag1.Noyau.Bagage;

import sgbag1.Vues.VueAeroport;

public class SimAnimator extends Thread {

    private static final int RUN = 10;
    private static final int PAUSE = 20;
    private static final int STOP = 30;
    int status;
    private static final int SLEEP_TIME = 200;
    private static final int BAGAGE_PERIOD = 50;
    private VueAeroport vueAeroport;
    private Aeroport aeroport;
    private boolean auto;

    private GraphicAeroport graphicAirport;


    public SimAnimator (VueAeroport va,Aeroport ap,GraphicAeroport ga) {
        super();
        vueAeroport = va;
        aeroport = ap;
        graphicAirport = ga;
        
   
    }

    public void setAuto(boolean val){
        auto = val;
        if(auto){
            System.out.println("Auto cruise : on");
        }
        else
            {
         System.out.println("Auto cruise : off");
        }
    }



    public VueAeroport addVueAeroport(VueAeroport va){
        vueAeroport = va;
        //System.out.println(vueAeroport.getClass().toString());
        return va;
    }

    public Aeroport addAeroport(Aeroport ap){
        aeroport = ap;
        return ap;
    }

    public GraphicAeroport addgraphicAirport(GraphicAeroport ga){
        graphicAirport = ga;
        return ga;
    }


    public int getStatus(){
        return status;

    }

    @Override
    public void run() {
        status = RUN;
        
        System.out.println("panel size: "+graphicAirport.getWidth()+" X " +graphicAirport.getHeight());
       
        int i = 0;
        



        while (status != STOP) {

            if (status == RUN) {

                //evolution du simulation

                if((i > BAGAGE_PERIOD )&& auto){

                    Random generator = new Random();
                    int num1 ,num2;

                    num1 = generator.nextInt(aeroport.getListeGuichets().size());
                    num2 = generator.nextInt(aeroport.getListeToboggans().size());
                    System.out.println("bagage : guichet "+ num1 + " toboggan "+ num2);
                    Bagage b = new Bagage(aeroport.getListeGuichets().get(num1).getTapis(), aeroport.getListeToboggans().get(num2));
                    aeroport.nouveauBagage(b);
                    i=0;
                }


                aeroport.avancer();
                vueAeroport.setTailleFenetre(graphicAirport.getWidth(),graphicAirport.getHeight());
                //vueAeroport.Dessiner(graphicAirport.getGraphics());
                
                graphicAirport.repaint();
                i++;
            }

            if (status == PAUSE) {
                // action periodique dans pause

            }




            try {
                Thread.sleep(SLEEP_TIME);
            } catch (InterruptedException ex) {
                Logger.getLogger(SimAnimator.class.getName()).log(Level.SEVERE, null, ex);
            }




        }
        
        System.out.println("Simulator Stopped");






    }

    public void Start() {
        
        status = RUN;
    }

    public void Pause() {
        status = PAUSE;
    }

    public void Stop() {
        
        status = STOP;
    }

    public void avancer(){
                aeroport.avancer();
                vueAeroport.Dessiner(graphicAirport.getGraphics());
                //graphicAirport.repaint();
    }

}
